/**
 * 创建时间： 2016年4月26日 下午2:55:39
 * @author itbegin
 * @version 1.0
 * 文件名称: UserDaoImpl.java
 * 类说明：
 *
 */
package com.itbegin.dao.impl;

import java.sql.PreparedStatement;
import java.sql.SQLException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.PreparedStatementSetter;
import org.springframework.stereotype.Repository;

import com.itbegin.dao.UserDao;
import com.itbegin.model.User;

@Repository("userDao")
public class UserDaoImpl implements UserDao {
	@Autowired
	private JdbcTemplate jdbcTemplate;
	
	@Override
	public User getUser(String name) {
		String sql = "select id,name,pwd,email,phone from user where name=?";
		try{
			User user = jdbcTemplate.queryForObject(sql, new Object[]{name},new BeanPropertyRowMapper<User>(User.class));
			return user;
		}catch(EmptyResultDataAccessException ex){
			return null;
		}
		
	}

	@Override
	public void addUser(final User user) {
		String sql = "insert into user (name,email,pwd,phone) values (?,?,?,?)";
		jdbcTemplate.update(sql, new PreparedStatementSetter(){

			@Override
			public void setValues(PreparedStatement ps)
					throws SQLException {
					ps.setString(1,user.getName());
					ps.setString(2, user.getEmail());
					ps.setString(3, user.getPwd());
					ps.setString(4, user.getPhone());
			}
			
		});
	}

}

